package com.yq.servlet;

import com.sun.org.apache.regexp.internal.REUtil;
import com.yq.bean.Product;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/product/*")
public class ServletProductServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/product/", "");
        if ("index".equals(op)){
            index(request,response);
        }else if ("info".equals(op)){
            info(request,response);
        }else if ("view".equals(op)){
            view(request,response);
        }

    }
    private void view(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //浏览记录
        ArrayList<String> ids = (ArrayList<String>) request.getSession().getAttribute("ids");
        if (ids==null){
            response.sendRedirect(request.getContextPath()+"/product/index");
            return;
        }
        ArrayList<Product> products = (ArrayList<Product>) getServletContext().getAttribute("products");
        response.getWriter().println("浏览商品的顺序");
        for (int i = 0; i < 3; i++) {
            String id = ids.get(i);
            for (Product product : products) {
                if (id.equals(product.getId())){
                    response.getWriter().println(product.getName());
                }

            }
        }
        response.getWriter().println("<a href="+request.getContextPath()+"/product/index"+">返回首页</a>");

    }

    private void info(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        if (id==null || "".equals(id)){
            response.sendRedirect(request.getContextPath()+"/product/index");
            return;
        }
        ArrayList<Product> products = (ArrayList<Product>) getServletContext().getAttribute("products");
        for (Product product : products) {
            if (product.getId().equals(id)){
                response.getWriter().println(product);
            }
        }
        //设置集合从头部插入
        HttpSession session = request.getSession();

        ArrayList<String> ids = (ArrayList<String>) request.getSession().getAttribute("ids");
        if (ids==null){
            ids= new ArrayList<>();
            session.setAttribute("ids",ids);
        }
        if (ids.size()>=3){
            ids.remove(ids.size()-1);
        }
        ids.add(0,id);
        response.getWriter().println("<a href="+request.getContextPath()+"/product/view"+">浏览记录</a>");
        response.getWriter().println("<a href="+request.getContextPath()+"/product/index"+">返回首页</a>");

    }

    private void index(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ArrayList<Product> products = (ArrayList<Product>) getServletContext().getAttribute("products");
        for (Product product : products) {
            response.getWriter().println("<a href="+request.getContextPath()+"/product/info?id="+product.getId()+">"+product.getName()+"</a>");
        }
    }
}
